package com.pigdogbay.androidutils.utils;

public class Supergram {

	int[] _LetterCounts = new int[26];
	char[] _Buffer = new char[64];
	int[] _SubsetIndices;
	
	public Supergram(String subset){
		int len = subset.length();
		subset.getChars(0, len, _Buffer, 0);
		_SubsetIndices = new int[len];
		for(int i=0;i<len;i++)
		{
			_SubsetIndices[i] = _Buffer[i]-'a';
		}
	}
	
	public boolean isSupergram(String superset)
	{
		countLetters(superset);
		int index;
		for (int i=0; i<_SubsetIndices.length;i++)
		{
			index = _SubsetIndices[i];
			if (_LetterCounts[index]==0)
			{
				return false;
			}
			_LetterCounts[index]--;
		}
		return true;
	}
	private void countLetters(String word)
	{
		int len = word.length();
		word.getChars(0, len, _Buffer, 0);
		for (int i=0;i<26;i++){
			_LetterCounts[i]=0;
		}
		int index;
		for (int i=0;i<len;i++)
		{
			index = _Buffer[i]-'a';
			_LetterCounts[index]++;
		}
	}
	
}
